package com.cneport.das.server.util.receive;

import com.cneport.das.common.utils.JAXBUtils;
import com.cneport.das.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * @author GavinZhang
 * @version 1.0
 * @desc： 消息监听者
 * @company 东方物通科技有限（北京）公司
 * @date 2021/9/9 14:38
 */
@Slf4j
public class MqMessageListener implements MessageListener {
    public String getMarshallerVoPath() {
        return marshallerVoPath;
    }

    public void setMarshallerVoPath(String marshallerVoPath) {
        this.marshallerVoPath = marshallerVoPath;
    }

    private String marshallerVoPath;

    public Handler getHandler() {
        return handler;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    private Handler handler;

    @Override
    public void onMessage(Message message) {
        String msg = "";
        if (message instanceof TextMessage) {
            TextMessage tm = (TextMessage) message;
            try {
                msg = tm.getText();
                log.info("收到消息：" + msg);
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
        if (!StringUtil.isBlank(msg)) {
            Object obj = JAXBUtils.xml2Obj(msg, getMarshallerVoPath());
            handler.handle(obj);
        }

    }
}
